package scraper

import (
	"context"

	"database/sql"

	"github.com/prometheus/client_golang/prometheus"
	"go.uber.org/zap"
)

var diskSQL = `SELECT TOTAL_DISK_SIZE , FREE_DISK_SIZE FROM V$SYSTEMINFO`

var scraperDiskTotalSizeDesc = prometheus.NewDesc(
	prometheus.BuildFQName(Namespace, Exporter, "collect_disk_total_size"),
	"Collect Disk Total Size",
	[]string{"collector"},
	nil,
)

var scraperDiskFreeSizeDesc = prometheus.NewDesc(
	prometheus.BuildFQName(Namespace, Exporter, "collect_disk_free_size"),
	"Collect Disk Free Size",
	[]string{"collector"},
	nil,
)

type ScrapeDisk struct{}

func (ScrapeDisk) Name() string {
	return "disk_size"
}

func (ScrapeDisk) Usage() string {
	return "Current Disk Size of kingbaseDB"
}

func (ScrapeDisk) Scraper(ctx context.Context, db *sql.DB, ch chan<- prometheus.Metric, logger *zap.Logger) error {
	total, free := 0.0, 0.0
	err := db.QueryRow(diskSQL).Scan(&total, &free)
	if err != nil {
		logger.Sugar().Errorf("Failed to scrape disk info: %v", err)
		return err
	}
	ch <- prometheus.MustNewConstMetric(
		scraperDiskTotalSizeDesc, prometheus.GaugeValue, total, "connection",
	)
	ch <- prometheus.MustNewConstMetric(
		scraperDiskFreeSizeDesc, prometheus.GaugeValue, free, "connection",
	)
	return nil
}

var _ DBScraper = (*ScrapeDisk)(nil)
